package com.flyco.modules.returns.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.modules.returns.entity.OmsOrderReturnItem;
import com.flyco.modules.returns.entity.OmsOrderReturnRealItem;
import com.flyco.modules.returns.mapper.OmsOrderReturnRealItemMapper;
import com.flyco.modules.returns.service.IOmsOrderReturnItemService;
import com.flyco.modules.returns.service.IOmsOrderReturnRealItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 实际退换货明细
 * @Author: flyco
 * @Date: 2023-08-01
 * @Version: V1.0
 */
@Service
public class OmsOrderReturnRealItemServiceImpl extends ServiceImpl<OmsOrderReturnRealItemMapper, OmsOrderReturnRealItem> implements IOmsOrderReturnRealItemService {

    @Autowired
    private OmsOrderReturnRealItemMapper returnRealItemMapper;
    @Autowired
    private IOmsOrderReturnItemService returnItemService;

    @Override
    public List<OmsOrderReturnRealItem> getByApplyId(Long applyId) {

        LambdaQueryWrapper<OmsOrderReturnRealItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsOrderReturnRealItem::getReturnApplyId, applyId);
        return list(wrapper);
    }

    @Override
    public void savaByApplyId(Long applyId){

        List<OmsOrderReturnItem> returnItemList = returnItemService.getByApplyId(applyId);
        List<OmsOrderReturnRealItem> returnRealItems=new ArrayList<>();
        for (OmsOrderReturnItem item:returnItemList){

            OmsOrderReturnRealItem returnRealItem=new OmsOrderReturnRealItem();
            BeanUtils.copyProperties(item,returnRealItem);
            returnRealItem.setMinUnitQuantity(returnRealItem.getReuseUnitQuantity()+returnRealItem.getUselessUnitQuantity());
            returnRealItems.add(returnRealItem);
        }
        saveOrUpdateBatch(returnRealItems);
    }
}
